package com.googlecode.pacswitch;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * @author Andrey Pavlenko
 */
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "config", namespace = ObjectFactory.NS)
@XmlType(name = "Config", propOrder = { "properties", "bindings", "log", "rules" })
public class Config {
    private Log _log;
    private final List<Property> _properties = Collections.checkedList(
            new ArrayList<Property>(), Property.class);
    private final List<Bind> _bindings = Collections.checkedList(
            new ArrayList<Bind>(), Bind.class);
    private final List<Rule> _rules = Collections.checkedList(
            new ArrayList<Rule>(), Rule.class);

    @XmlElement(name = "log")
    public Log getLog() {
        return _log;
    }

    public void setLog(final Log log) {
        _log = log;
    }

    @XmlElement(name = "property", type = Property.class)
    public List<Property> getProperties() {
        return _properties;
    }

    @XmlElement(name = "bind", type = Bind.class)
    public List<Bind> getBindings() {
        return _bindings;
    }

    @XmlElementWrapper(name = "rules")
    @XmlElements(@XmlElement(name = "rule", type = Rule.class))
    public List<Rule> getRules() {
        return _rules;
    }

    public Property getProperty(final String name) {
        for (final Property p : _properties) {
            if (name.equals(p.getName())) {
                return p;
            }
        }

        final String value = System.getProperty(name);
        return new Property(name, (value == null ? null : new PString(value)));
    }
}
